/*
 * @Author: 260156 260156@gree.com.cn
 * @Date: 2022-07-25 13:48:51
 * @LastEditors: 260156 260156@gree.com.cn
 * @LastEditTime: 2022-11-25 11:03:11
 * @FilePath: \ly-vue3-framework\vite.config.ts
 * @Description:
 *
 * Copyright (c) 2022 by 260156 260156@gree.com.cn, All Rights Reserved.
 */
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import ElementPlus from "unplugin-element-plus/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import vueSetupExtend from "vite-plugin-vue-setup-extend";

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const { VITE_PROXY, VITE_PUBLIC_PATH } = loadEnv(mode, process.cwd(), "");
  return {
    base: VITE_PUBLIC_PATH,
    resolve: {
      alias: {
        "@/": `${resolve(__dirname, "src")}/`,
        "#/": `${resolve(__dirname, "types")}/`,
        "views/": `${resolve(__dirname, "src/views")}/`,
        "utils/": `${resolve(__dirname, "src/utils")}/`,
        "styles/": `${resolve(__dirname, "src/styles")}/`,
        "store/": `${resolve(__dirname, "src/store")}/`,
        "api/": `${resolve(__dirname, "src/api")}/`,
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/styles/element/index.scss" as *; @use "@/styles/index.scss" as *;`,
        },
      },
    },
    server: {
      host: true,
      port: 3001,
      open: true,
      proxy: {
        "/api": {
          target: VITE_PROXY,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
    plugins: [
      vue(),
      // 自动引入使用到的element组件
      ElementPlus({
        useSource: true,
      }),
      // 自动导入 Vite、Webpack、Rollup 和 esbuild 的 API
      AutoImport({
        imports: ["vue", "@vueuse/core"],
        resolvers: [ElementPlusResolver()],
        vueTemplate: true,
        dts: true,
        eslintrc: {
          enabled: true,
        },
      }),
      // 自动导入和注册组件，无需手动引入element plus组件
      Components({
        resolvers: [
          ElementPlusResolver({
            importStyle: "sass",
          }),
        ],
        dts: true,
      }),
      vueSetupExtend(),
    ],
    build: {
      chunkSizeWarningLimit: 1500,
      rollupOptions: {
        output: {
          chunkFileNames: "js/[name]-[hash].js",
          entryFileNames: "js/[name]-[hash].js",
          assetFileNames: "[ext]/[name]-[hash].[ext]",
          manualChunks: {
            echarts: ["echarts"],
            lodash: ["lodash"],
          },
        },
      },
    },
  };
});
